#!/usr/bin/python

# This is used to query all required data with the snmp_query module and then update to rrd database
#   And there will be a cronjob running to collect data and update to the rrd database every 5 minutes

import time
import os
import sys
import rrdtool

from utils import snmp_query

ip = "127.0.0.1"
community = "public"
file_location = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '../static/img')
timestamp = str(time.time())
myquery = snmp_query.QueryOidData(ip, community)


def update_rrd(rrddb_path, data):
    rrdtool.updatev(os.path.join(file_location, rrddb_path), data)


def main():
    cpu_util = myquery.query_cpuutil()
    update_rrd('cpu_util.rrd', timestamp + ':' + cpu_util)

    traffic = myquery.query_iftraffic()
    for interface in traffic.keys():
        inbound = traffic[interface]['in']
        outbound = traffic[interface]['out']
        update_rrd(interface + '.rrd', timestamp + ':' + inbound + ':' + outbound)

if __name__ == "__main__":
    main()


